%% 连接SVG模型 - 基于实际端口信息
function connect_svg_model(model_name)
    if nargin < 1
        model_name = 'SVG_Star_10kV_Complete';
    end
    
    % 确保模型已打开
    if ~bdIsLoaded(model_name)
        open_system(model_name);
    end
    
    fprintf('开始连接SVG模型...\n\n');
    
    %% 1. 连接电网系统
    fprintf('1. 连接电网系统...\n');
    
    % Strong_Grid -> Strong_Grid_Z (三相连接)
    try
        for i = 1:3
            src_port = get_param([model_name '/Strong_Grid'], 'PortHandles');
            dst_port = get_param([model_name '/Strong_Grid_Z'], 'PortHandles');
            
            if length(src_port.RConn) >= i && length(dst_port.LConn) >= i
                add_line(model_name, src_port.RConn(i), dst_port.LConn(i), 'autorouting', 'on');
                fprintf('   已连接: Strong_Grid RConn%d -> Strong_Grid_Z LConn%d\n', i, i);
            end
        end
    catch ME
        fprintf('   错误: Strong_Grid -> Strong_Grid_Z: %s\n', ME.message);
    end
    
    % Weak_Grid_PV -> Weak_Grid_Z (三相连接)
    try
        for i = 1:3
            src_port = get_param([model_name '/Weak_Grid_PV'], 'PortHandles');
            dst_port = get_param([model_name '/Weak_Grid_Z'], 'PortHandles');
            
            if length(src_port.RConn) >= i && length(dst_port.LConn) >= i
                add_line(model_name, src_port.RConn(i), dst_port.LConn(i), 'autorouting', 'on');
                fprintf('   已连接: Weak_Grid_PV RConn%d -> Weak_Grid_Z LConn%d\n', i, i);
            end
        end
    catch ME
        fprintf('   错误: Weak_Grid_PV -> Weak_Grid_Z: %s\n', ME.message);
    end
    
    % Strong_Grid_Z -> Grid_Selector 第一组输入
    try
        for i = 1:3
            src_port = get_param([model_name '/Strong_Grid_Z'], 'PortHandles');
            dst_port = get_param([model_name '/Grid_Selector'], 'PortHandles');
            
            if length(src_port.RConn) >= i && length(dst_port.LConn) >= i
                add_line(model_name, src_port.RConn(i), dst_port.LConn(i), 'autorouting', 'on');
                fprintf('   已连接: Strong_Grid_Z RConn%d -> Grid_Selector LConn%d\n', i, i);
            end
        end
    catch ME
        fprintf('   错误: Strong_Grid_Z -> Grid_Selector: %s\n', ME.message);
    end
    
    % 注意：Weak_Grid_Z 应该连接到其他地方或者Grid_Selector的第二组输入
    % 但Grid_Selector只有3个输入，所以可能需要修改Grid_Selector的设计
    
    %% 2. 连接Grid_Selector到Star_SVG
    fprintf('\n2. 连接电网选择器到SVG...\n');
    
    % Grid_Selector是电力模块，Star_SVG是子系统，需要特殊处理
    % 首先检查Star_SVG内部是否有合适的电力端口
    
    try
        % 获取Grid_Selector的输出端口
        grid_sel_ports = get_param([model_name '/Grid_Selector'], 'PortHandles');
        
        % 检查Star_SVG的输入端口类型
        svg_ports = get_param([model_name '/Star_SVG'], 'PortHandles');
        
        if ~isempty(svg_ports.Inport)
            % Star_SVG有信号输入端口，可能需要转换
            fprintf('   警告: Star_SVG使用信号端口，需要添加电力-信号转换\n');
            fprintf('   建议在Star_SVG内部添加三相电力输入端口\n');
        end
        
        % 尝试直接连接（如果Star_SVG内部有正确的端口）
        % 这里需要根据实际情况调整
        
    catch ME
        fprintf('   错误: Grid_Selector -> Star_SVG: %s\n', ME.message);
    end
    
    %% 3. 连接Mixed_Load
    fprintf('\n3. 处理负载连接...\n');
    
    % Mixed_Load当前没有端口，需要先添加
    fprintf('   Mixed_Load需要添加三相输入端口\n');
    fprintf('   建议在Mixed_Load子系统中添加三相电力输入端口\n');
    
    %% 4. 添加测量系统连接
    fprintf('\n4. 测量系统连接...\n');
    fprintf('   Measurements子系统需要添加相应的端口\n');
    
    %% 5. 验证连接状态
    fprintf('\n5. 验证连接状态...\n');
    verify_connections(model_name);
    
    fprintf('\n连接脚本执行完成！\n');
    fprintf('\n建议的后续步骤：\n');
    fprintf('1. 在Star_SVG子系统内添加三相电力输入端口（从Grid_Connection Inport连接到内部）\n');
    fprintf('2. 在Mixed_Load子系统内添加三相电力输入端口\n');
    fprintf('3. 在Measurements子系统内添加必要的测量端口\n');
    fprintf('4. 完善Grid_Selector的设计（如果需要切换两个电网）\n');
end

%% 辅助函数：验证连接状态
function verify_connections(model_name)
    blocks = find_system(model_name, 'SearchDepth', 1);
    unconnected = 0;
    
    for i = 1:length(blocks)
        block_path = blocks{i};
        if strcmp(block_path, model_name)
            continue;
        end
        
        block_name = get_param(block_path, 'Name');
        
        try
            handles = get_param(block_path, 'PortHandles');
            
            % 检查电力端口
            if isfield(handles, 'LConn')
                for j = 1:length(handles.LConn)
                    if get_param(handles.LConn(j), 'Line') == -1
                        fprintf('   未连接: %s LConn%d\n', block_name, j);
                        unconnected = unconnected + 1;
                    end
                end
            end
            
            if isfield(handles, 'RConn')
                for j = 1:length(handles.RConn)
                    if get_param(handles.RConn(j), 'Line') == -1
                        fprintf('   未连接: %s RConn%d\n', block_name, j);
                        unconnected = unconnected + 1;
                    end
                end
            end
            
            % 检查信号端口
            if isfield(handles, 'Inport')
                for j = 1:length(handles.Inport)
                    if get_param(handles.Inport(j), 'Line') == -1
                        fprintf('   未连接: %s Inport%d\n', block_name, j);
                        unconnected = unconnected + 1;
                    end
                end
            end
            
            if isfield(handles, 'Outport')
                for j = 1:length(handles.Outport)
                    if get_param(handles.Outport(j), 'Line') == -1 && ~contains(block_name, 'Display')
                        fprintf('   未连接: %s Outport%d\n', block_name, j);
                        unconnected = unconnected + 1;
                    end
                end
            end
        catch
            % 忽略错误
        end
    end
    
    fprintf('   总计 %d 个端口未连接\n', unconnected);
end